<?php
class Web_View_Helper_CommerceSchedules
	extends Zend_View_Helper_Abstract 
{
	
	public function commerceSchedules( $horarios ) 
	{
		
		
		
		$days = array();
		
		foreach ( $horarios as $horario){
		
			$dayName = $horario->getDayName();
			
			if ( !isset( $days[$dayName] )){
				$days[$dayName] = "{$this->cut($horario->desde)} " . $this->view->translate('to') . " {$this->cut($horario->hasta)}"; 
			} else {
				$days[$dayName].= ' ' . $this->view->translate('and') . " {$this->cut($horario->desde)} ".$this->view->translate('to')." {$this->cut($horario->hasta)}"; 
			}
			
			
			
		}
		
		$daysName = $this->getDays();
		
		array_push($daysName, array_shift($daysName));
		
		$html = "
			<table>
				<thead>
					<tr>
						<th colspan='2'>".$this->view->translate('day')."</th>
						<th colspan='2'>".$this->view->translate('OpeningClosingHours')."</th>
					</tr>
				</thead>
				<tbody>";
		
        $now = new Zend_Date();
		
        $todayName = ucfirst($now->toString(Zend_Date::WEEKDAY));
        
        $t = count($daysName);
        $n = 1;
        
        foreach ( $daysName as $dayName){
        	
        	if ( $dayName == $todayName ){
        		$class = ' class="today"';
        	} elseif( $n == $t ) {
        		$class = ' class="last"';
        	}else{
        		$class = null;
        	}
        	
        	
        	$html.="
        	<tr{$class}>
        		<td class='left'></td>
        		<td>{$dayName}</td>
        		<td>" . ( isset( $days[$dayName]) ? $days[$dayName] : $this->view->translate('closed') ). "</td>
        		<td class='right'></td>
        	</tr>
        	";
        	
        	$n++;
        	
        }
        
        $html.="
        	</tbody>
        	</table>
        	";
		
		return $html;
	}

	public function cut ( $str )
	{
		return substr( $str, 0,5);
	}
	
	public function getDays()
	{
		$list = Zend_Locale::getTranslationList('days');		
		$days = $list['format']['wide'];
		
		return array_map('ucfirst', $days);
	}
}